package com.fitso.wicket;

import org.apache.log4j.Logger;
import org.apache.log4j.NDC;
import org.apache.wicket.Response;
import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.protocol.http.WebRequest;
import org.apache.wicket.protocol.http.WebRequestCycle;

/**
 * Logs requests uniquely
 * 
 * @author timothystorm
 */
public class LoggingWebRequestCycle extends WebRequestCycle {

	private static final Logger logger = Logger.getLogger(LoggingWebRequestCycle.class);

	public LoggingWebRequestCycle(WebApplication application, WebRequest request, Response response) {
		super(application, request, response);
	}

	/**
	 * Pushes an NDC with the unique session ID
	 */
	@Override
	protected void onBeginRequest() {
		super.onBeginRequest();
		NDC.push(FitsoSession.get().getId());
		logger.info("beginning request...");
	}

	/**
	 * Pops an NDC
	 */
	@Override
	protected void onEndRequest() {
		super.onEndRequest();
		logger.info("ending request.");
		NDC.pop();
	}
}
